package com.squirrel.michale;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/6 7:42 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1578 {

    public int minCost(String colors, int[] neededTime) {

        int costTime = 0;
        List<Balloons> balloonsList = new ArrayList<>();
        PriorityQueue<Balloons> colorHeap = new PriorityQueue<>(new Comparator<Balloons>() {
            @Override
            public int compare(Balloons o1, Balloons o2) {
                return o1.needTime-o2.needTime;
            }
        });
        char[] chars = colors.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            balloonsList.add(new Balloons(chars[i], neededTime[i]));
        }

        for (int i = 0; i < balloonsList.size(); i++) {
            if (null == colorHeap.peek() || colorHeap.peek().color == balloonsList.get(i).color) {
                colorHeap.add(balloonsList.get(i));
            }else{
                while (colorHeap.size() > 1) {
                    Balloons balloons = colorHeap.poll();
                    costTime += balloons.needTime;
                }
                colorHeap.clear();
                colorHeap.add(balloonsList.get(i));
            }
        }

        while (colorHeap.size() > 1) {
            Balloons balloons = colorHeap.poll();
            costTime += balloons.needTime;
        }
        return costTime;
    }


    class Balloons{
        Character color;

        int needTime;

        public Balloons(Character color, int needTime) {
            this.color = color;
            this.needTime = needTime;
        }
    }

    public static void main(String[] args) {

        LeetCode1578 leetCode1578 = new LeetCode1578();

        String colors = "aabaa";
        int[] neededTime =new int[] {1,2,3,4,1};
        System.out.println(leetCode1578.minCost(colors, neededTime));


        String colors2 = "abaac";
        int[] neededTime2 = new int[]{1, 2, 3, 4, 5};
        System.out.println(leetCode1578.minCost(colors2, neededTime2));
    }
}
